$(function(){
	var ajax = new AjaxOperator();
	var tabOperator = new TableOperator();
	$("#add").on("click", function(){
		var name=$("#name").val();
		var sex=$("input[type='radio']:checked").val();
		var sal=$("#sal").val();
		var birth=$("#birth").val();
		var user = {name:name,sex:sex,sal:sal,birth:birth};
		ajax.sendPost("addUser", user, addDoneFn, fnfail);
	});
	
	var addDoneFn = function(msg){
		tabOperator.createTable($("#tab"), msg);
		tabOperator.bindDelEvent("del",delDoneFn,fnfail);
		tabOperator.bindUpdateEvent();
	}
	var delDoneFn = function(msg){
		if(msg=="fail"){
			alert("delete failed!");
		}else{
			var selector = "button[uid="+msg+"]";
			$(selector).parent().parent().remove();
		}
	}
	var fnfail = function(){
		alert("operate failed!");
	}
	
	$("#submit_update").on("click", function(){
		var uid = $("#modal_id").val();
		var name = $("#modal_name").val();
		var a = $("input[name=sex]:checked").val();
		if(a=="checked"){
			var sex=true;
		}else{
			var sex=false;
		}
		var sal = $("#modal_sal").val();
		var birth = $("#modal_birth").val();
		var user={id:uid,name:name,sex:sex,sal:sal,birth:birth};
		ajax.sendPost("update", user, updateDoneFn, fnfail);
	});
	var updateDoneFn = function(msg){
		$("#myModal").modal('hide');
		var selector="button[uid="+msg.id+"]";
		$(selector).parent().parent().remove();
		tabOperator.addTableRow($("#tab"),msg);
	}
			
});